/*
 * Copyright (c) 2019, Zhejiang uniview Technologies Co., Ltd. All rights reserved.
 * <http://www.uniview.com/>
 *------------------------------------------------------------------------------
 * Product     :
 * Module Name :
 * Date Created: 2020-04-07 10:42
 * Creator     : s04180
 * Description :
 *
 *------------------------------------------------------------------------------
 * Modification History
 * DATE        NAME             DESCRIPTION
 *------------------------------------------------------------------------------
 *
 *------------------------------------------------------------------------------
 */
package com.sparrow.source.socket.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sparrow.source.socket.constant.LapiUriConstat;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * HttpConnectionThread
 *
 * @author s04180
 * @Description
 */
@Component
public class RequestDeviceTestThread implements Runnable {

	private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(RequestDeviceTestThread.class);

	//终端接口地址URI
	private String api = LapiUriConstat.PEOPLE_BASIC_INFO_URI;

	//终端设备的序列号 or  响应心跳的 deviceCode
	private String serialNo = "210235C8MG3249000022";

	//请求的接口参数
	private String json = "";

	@Override
	public void run() {

		try {
			for (; ; ) {
				FullHttpRequest httpRequest = null;

				//POST
				// httpRequest =  RequestDeviceManager.createRequestDeviceInfo(api, json,HttpMethod.POST);

				//DELETE
				// httpRequest =RequestDeviceManager.createRequestDeviceInfo(api, json,HttpMethod.DELETE);

				//PUT
				// httpRequest =RequestDeviceManager.createRequestDeviceInfo(api, json,HttpMethod.PUT);

				//GET
				httpRequest = RequestDeviceManager.createRequestDeviceInfo(api, json, HttpMethod.GET);

				/**
				 *
				 */
				Map<String, String> map = HttpKeepAliveManager.getResponseMapByKeepAliveConnection(serialNo, httpRequest);
				if (Objects.nonNull(map)) {
//					LOGGER.info("Device:{} Response Info:\n{}", serialNo, JSON.toJSONString(map, SerializerFeature.PrettyFormat));
				} else {
					LOGGER.warn("... ... 请求超时 ... ... ");
				}
				// 请求休眠10s
				Thread.sleep(10000);
			}
		} catch (Exception e) {
			LOGGER.error("{}", e);
		}
	}
}
